package com.abc.robot01.vivemodel

import android.util.Log
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abc.robot01.retrofit.RetrofitService
import com.abc.robot01.retrofit.bean.Condition20
import com.abc.robot01.retrofit.bean.FetchDataRequest9
import com.abc.robot01.retrofit.bean.HousePlanDataDTO
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import org.json.JSONObject

class HuXingShuJuViewModel : ViewModel() {
    var housePlanDataId: String=""
    val TAG = "HuXingShuJuViewModel"
    val expanded1 =  mutableStateOf(false)
    val expanded2 =  mutableStateOf(false)
    val selected10 =  mutableStateOf("请选择")
    val selected20 =  mutableStateOf("请选择")
    val remark =  mutableStateOf("")
    // MutableStateFlow 用来保存当前的列表状态
    private val _questionList = MutableStateFlow<List<HousePlanDataDTO>>(emptyList())
    val questionList: StateFlow<List<HousePlanDataDTO>> = _questionList
    val name = mutableStateOf("")
    val designPackageId = mutableStateOf("")
    val housePlanId = mutableStateOf("")

    private val _designerList = MutableStateFlow<List<Pair<String, String>>>(emptyList())
    val designerList: StateFlow<List<Pair<String, String>>> = _designerList

    //当前选择的户型
    val selectedHouseType = mutableStateOf("")

    var selectedIndex = mutableStateOf(-1)

    fun reset(){
        name.value = ""
        remark.value = ""
        selectedIndex.value = -1 ;
        selected1.value = false
        selected2.value = false
    }

    //当前选择的创建者
    val selectedDesigner = mutableStateOf("")

    val selected1 =  mutableStateOf(false)
    val selected2 =  mutableStateOf(false)
//    var housePlanId=""
    var thumbnail=""
    // MutableStateFlow 用来保存当前的列表状态
//    private val _houseTypeList = MutableStateFlow<List<HouseType>>(emptyList())
//    val houseTypeList: StateFlow<List<HouseType>> = _houseTypeList

//    private val _designerList = MutableStateFlow<List<Pair<String, String>>>(emptyList())
//    val designerList: StateFlow<List<Pair<String, String>>> = _designerList

    fun loadData(
                 houseType: String = selectedHouseType.value,
                 selectedDesigner2:String= selectedDesigner.value) {
        viewModelScope.launch {
            _questionList.value = emptyList()
           val res = RetrofitService.instance.getHousePlanData(accessToken = Robot.accessToken,
               housePlanId = housePlanId.value,
               userId = Robot.userId,
               companyId = Robot.companyId,
               request= FetchDataRequest9(Condition20(name=name.value,remarks=remark.value))
            ).string()

            Log.e(TAG, "RetrofitService res:$res")
            val obj = JSONObject(res)
            val code = obj.getInt("code")
            Log.e(TAG, "RetrofitService code:$code")
            val list = obj.getJSONObject("data").getJSONArray("list")
            Log.e(TAG, "RetrofitService list:${list.length()}")

            val itemList = mutableListOf<HousePlanDataDTO>()

            // 如果你只想打印特定字段，比如 "name"
            for (i in 0 until list.length()) {
                val item = list.getJSONObject(i)
                val id = item.optString("id", "No id") // 默认值为 "No Name"
                val name = item.optString("name", "No Name") // 默认值为 "No Name"
                val remarks = item.optString("remarks", "No remarks") // 默认值为 "No Name"
                Log.e(TAG, "RetrofitService name:$name")
                Log.e(TAG, "RetrofitService remarks:$remarks")
                val imageUrl = item.optString("imageUrl", "No imageUrl") // 默认值为 "No Name"
                Log.e(TAG, "RetrofitService imageUrl:$imageUrl")
                itemList.add(HousePlanDataDTO(id=id,name=name,imageUrl=imageUrl,remarks=remarks))
            }
            _questionList.value = itemList
        }
    }
}
